package cn.com.taurus.controller.major;

import cn.com.taurus.dispose.UserSecurityService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class SecurityController {

    @RequestMapping(value = "/login",produces = {"text/plain;charset=UTF-8"})
    public ModelAndView home(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("major/login");

        return modelAndView;
    }

    @RequestMapping(value = "/kkp",produces = {"text/plain;charset=UTF-8"})
    public ModelAndView users(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("supermanager/BackgroundHomepage");
        return modelAndView;
    }

    @RequestMapping(value = "/major/roles",produces = {"text/plain;charset=UTF-8"})
    @PreAuthorize("hasAnyAuthority('admin','superAdmin')")
    public ModelAndView roles(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("major/fundManager");
        return modelAndView;
    }

    @RequestMapping(value = "/major/user",produces = {"text/plain;charset=UTF-8"})
    @PreAuthorize("hasAnyAuthority('admin','superAdmin')")
    public ModelAndView user(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("major/menus");
        return modelAndView;
    }

    @RequestMapping(value = "/finance/roles",produces = {"text/plain;charset=UTF-8"})
    @PreAuthorize("hasAnyAuthority('finance','superAdmin')")
    public ModelAndView menus(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("finance/Test");
        return modelAndView;
    }


    @RequestMapping(value = "/superAdmin/tyu",produces = {"text/plain;charset=UTF-8"})
    @PreAuthorize("hasAuthority('superAdmin')")
    public ModelAndView tyu(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("major/bottom");
        return modelAndView;
    }

    @RequestMapping(value = "/Anonymous/homepage",produces = {"text/plain;charset=UTF-8"})
    public ModelAndView others(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("manager/Test");
        return modelAndView;
    }

    //获取当前用户的信息
    private String getUsername(){
        String username = null;
        //当前认证通过的用户身份
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        //用户身份
        Object principal = authentication.getPrincipal();
        if (principal == null){
            username = "匿名";
        }
        if (principal instanceof UserDetails){
            UserDetails userDetails = (UserDetails) principal;
            username = userDetails.getUsername();
        }else {
            username = principal.toString();
        }
        return username;
    }
}
